package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Role;
import com.qk.mapper.RoleMapper;
import com.qk.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 角色列表
     */
    @Override
    public PageResult<Role> roleList(String name, String label, Integer page, Integer pageSize) {
        //1.设置PageHelper分页参数
        PageHelper.startPage(page, pageSize);
        //2.调用mapper接口方法，执行查询
        List<Role> roleList = roleMapper.selectList(name,label);
        //3.解析并封装分页结果
        Page<Role> p = (Page<Role>) roleList;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    /**
     * 根据id删除角色
     */
    @Override
    public void deleteById(Integer id) {
         roleMapper.deleteById(id);
    }

    /**
     * 添加角色
     */
    @Override
    public void add(Role role) {
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.add(role);
    }

    /**
     * 根据id查询角色
     */
    @Override
    public Role selectById(Integer id) {
        return roleMapper.selectById(id);
    }

    /**
     * 修改角色
     */
    @Override
    public void update(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.update(role);

    }

    /**
     * 查询所有角色
     */
    @Override
    public List<Role> selectAll() {
        return roleMapper.selectAll();
    }
}
